
package GUI;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.zip.Inflater;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import logic.Book;
import logic.Loan;
import logic.Message;

import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.JButton;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import java.io.IOException;

import javax.swing.JComboBox;
import javax.swing.UIManager;

public class LateReturnReport extends JPanel{
	private Client_GUI parent;
	private JTextField textFieldAveragehot;
	private JTextField textFieldMedianhot;
	private JTextField textField;
	private LateReturnReport self;
	private JLabel label;
	private JComboBox comboBox;
	private String startDate;
	private String endDate;
	
	
	public LateReturnReport(final Client_GUI parent,Message msg) {
		setBackground(UIManager.getColor("inactiveCaption"));
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.self = this;
		
		ArrayList<Object> data = (ArrayList<Object>)msg.GetMessageData();
		
		ArrayList<String> stat = (ArrayList<String>)data.get(0);
		String avg = (String)data.get(1);
		String med = (String)data.get(2);
		startDate = (String)data.get(3);
		endDate = (String)data.get(4);
		
		
		JLabel lblLoanReport = new JLabel("Late Return Report");
		lblLoanReport.setForeground(Color.RED);
		lblLoanReport.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLoanReport.setBounds(302, 39, 204, 40);
		add(lblLoanReport);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setBounds(28, 82, 369, 378);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("All Books");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel.setBounds(127, 11, 95, 22);
		panel.add(lblNewLabel);
		
		JLabel lblAveragehot = new JLabel("Average:");
		lblAveragehot.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblAveragehot.setBounds(49, 56, 81, 22);
		panel.add(lblAveragehot);
		
		textFieldAveragehot = new JTextField(avg);
		textFieldAveragehot.setEditable(false);
		textFieldAveragehot.setBounds(182, 57, 127, 22);
		panel.add(textFieldAveragehot);
		textFieldAveragehot.setColumns(10);
		
		JLabel lblMedian = new JLabel("Median:");
		lblMedian.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblMedian.setBounds(49, 89, 81, 22);
		panel.add(lblMedian);
		
		textFieldMedianhot = new JTextField(med);
		textFieldMedianhot.setEditable(false);
		textFieldMedianhot.setColumns(10);
		textFieldMedianhot.setBounds(182, 90, 127, 22);
		panel.add(textFieldMedianhot);
		
		JLabel lblDays = new JLabel("1 Days:");
		lblDays.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDays.setBackground(new Color(240, 240, 240));
		lblDays.setBounds(49, 165, 59, 22);
		panel.add(lblDays);
		
		JLabel lblDays_1 = new JLabel("2 Days:");
		lblDays_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDays_1.setBackground(SystemColor.menu);
		lblDays_1.setBounds(49, 198, 70, 22);
		panel.add(lblDays_1);
		
		JLabel lblDays_2 = new JLabel("3 Days:");
		lblDays_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDays_2.setBackground(SystemColor.menu);
		lblDays_2.setBounds(49, 232, 59, 22);
		panel.add(lblDays_2);
		
		JLabel lblDays_3 = new JLabel("4 Days:");
		lblDays_3.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDays_3.setBackground(SystemColor.menu);
		lblDays_3.setBounds(49, 265, 59, 22);
		panel.add(lblDays_3);
		
		JLabel lblDays_4 = new JLabel("5+ Days:");
		lblDays_4.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDays_4.setBackground(SystemColor.menu);
		lblDays_4.setBounds(49, 298, 59, 22);
		panel.add(lblDays_4);
		
		JLabel LBLall1 = new JLabel(stat.get(0));
		LBLall1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		LBLall1.setBounds(127, 165, 59, 19);
		panel.add(LBLall1);
		
		JLabel LBLall2 = new JLabel(stat.get(1));
		LBLall2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		LBLall2.setBounds(127, 203, 59, 19);
		panel.add(LBLall2);
		
		JLabel LBLall3 = new JLabel(stat.get(2));
		LBLall3.setFont(new Font("Tahoma", Font.PLAIN, 13));
		LBLall3.setBounds(127, 237, 59, 19);
		panel.add(LBLall3);
		
		JLabel LBLall4 = new JLabel(stat.get(3));
		LBLall4.setFont(new Font("Tahoma", Font.PLAIN, 13));
		LBLall4.setBounds(127, 270, 59, 19);
		panel.add(LBLall4);
		
		JLabel LBLall5 = new JLabel(stat.get(4));
		LBLall5.setFont(new Font("Tahoma", Font.PLAIN, 13));
		LBLall5.setBounds(127, 303, 59, 19);
		panel.add(LBLall5);
		
		JLabel label_8 = new JLabel("%");
		label_8.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_8.setBounds(194, 165, 18, 22);
		panel.add(label_8);
		
		JLabel label_9 = new JLabel("%");
		label_9.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_9.setBounds(196, 198, 18, 22);
		panel.add(label_9);
		
		JLabel label_10 = new JLabel("%");
		label_10.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_10.setBounds(196, 232, 18, 22);
		panel.add(label_10);
		
		JLabel label_11 = new JLabel("%");
		label_11.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_11.setBounds(196, 265, 18, 22);
		panel.add(label_11);
		
		JLabel label_12 = new JLabel("%");
		label_12.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_12.setBounds(196, 298, 18, 22);
		panel.add(label_12);
		
		JButton btnNewButton = new JButton("Done");
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Home home = new Home(parent,parent.client.publicMessages);
				VerifyDialog verify = new VerifyDialog(parent, home,false);

			}
		});
		btnNewButton.setBounds(353, 471, 103, 32);
		add(btnNewButton);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel_1.setBounds(407, 82, 379, 182);
		add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblByCategoryBooks = new JLabel("By Category ");
		lblByCategoryBooks.setBounds(142, 11, 103, 22);
		panel_1.add(lblByCategoryBooks);
		lblByCategoryBooks.setFont(new Font("Tahoma", Font.BOLD, 15));
		
		JLabel lblCategory = new JLabel("Category:");
		lblCategory.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCategory.setBounds(10, 87, 72, 14);
		panel_1.add(lblCategory);
		
		JButton button = new JButton("Get Report");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				ValidateData(true,"Category");
			}
		});
		button.setBounds(142, 148, 89, 23);
		panel_1.add(button);
		
		comboBox = new JComboBox();
		comboBox.setBounds(103, 81, 173, 20);
		panel_1.add(comboBox);
		
		JLabel label_1 = new JLabel("");
		label_1.setBounds(288, 83, 19, 18);
		panel_1.add(label_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel_2.setBounds(407, 275, 379, 182);
		add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblByBooks = new JLabel("By Books");
		lblByBooks.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblByBooks.setBounds(142, 11, 95, 22);
		panel_2.add(lblByBooks);
		
		JLabel lblNewLabel_1 = new JLabel("Book Id:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_1.setBounds(10, 84, 72, 14);
		panel_2.add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.setBounds(73, 80, 164, 22);
		panel_2.add(textField);
		textField.setColumns(10);
		
		final JLabel label = new JLabel("");
		label.setBounds(238, 84, 19, 18);
		panel_2.add(label);
		
		JButton btnNewButton_1 = new JButton("Search");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				SearchBook topaint = new SearchBook(parent, SearchBook.SEARCH_FOR_LATE_RETURN, self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
				
			}
		});
		btnNewButton_1.setBounds(267, 81, 89, 23);
		panel_2.add(btnNewButton_1);
		
		JButton btnGetReport = new JButton("Get Report");
		btnGetReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				ValidateData(true,"book");
					
			}
		});
		btnGetReport.setBounds(142, 134, 89, 23);
		panel_2.add(btnGetReport);
			
		LoadCategories();
	}
	
	
	/**
	 * This function sets the BID on the textfield.
	 * This function will get called after pressing
	 * the SearchMember button
	 * @param bid
	 */
	public void SetBID(String bid){
		textField.setText(bid);
	}

	
	/**
	 * This function will build the categories list inside the comboBox
	 */
	public void LoadCategories(){
		comboBox.insertItemAt(Book.CATEGORY_ARCHITECTURE, 0);
		comboBox.insertItemAt(Book.CATEGORY_BIOTECHNOLOGY, 1);
		comboBox.insertItemAt(Book.CATEGORY_BUILDING_ENGINEERING, 2);
		comboBox.insertItemAt(Book.CATEGORY_ELECTRONICS, 3);
		comboBox.insertItemAt(Book.CATEGORY_FINANCE, 4);
		comboBox.insertItemAt(Book.CATEGORY_HUMAN_RESOURCES, 5);
		comboBox.insertItemAt(Book.CATEGORY_INDUSTRIAL_ENGINEERING, 6);
		comboBox.insertItemAt(Book.CATEGORY_MATH, 7);
		comboBox.insertItemAt(Book.CATEGORY_MECHANICAL_ENGINEERING, 8);
		comboBox.insertItemAt(Book.CATEGORY_SOFTWARE_ENGINEERING, 9);
		comboBox.insertItemAt(Book.CATEGORY_TEACHING, 10);

	}
	
	
	private void ValidateData(boolean fromOK,String opp){
		
		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
		if(opp.equals("book"))
		{
			//Validate BOOK ID
			temp = textField.getText();
			if (!temp.equals(""))
			try{
				char c = temp.charAt(0);
				if (c == '+' || c == '-') {
					label.setIcon(BAD);
					valid = false;
				}//else label.setIcon(OK);
				Integer.parseInt(temp);	
			}catch(NumberFormatException e){
				label.setIcon(BAD);
				valid = false;
			}
			else {
				label.setIcon(BAD);
				valid = false;
			}
		}
		else
		{
			if(comboBox.getSelectedIndex()==-1)
			{
				label.setIcon(BAD);
				valid=false;
			}
			
		}
		
			
		if (valid && fromOK){
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
			int action = 0;
			
			parent.currentMain = this;
			
			if(opp.equals("book"))
			{
			index.add("BID");
			data.add(textField.getText());
			
			index.add("STARTDATE");
			data.add(startDate);
			index.add("ENDDATE");
			data.add(endDate);
			
			action = Message.LATE_RETURN_REPORT_BOOK_REQUEST;
			}
			else //category
			{
				
				index.add("CATEGORY");
				data.add(comboBox.getSelectedItem());
				
				index.add("STARTDATE");
				data.add(startDate);
				index.add("ENDDATE");
				data.add(endDate);
				
				
				
				action = Message.LATE_RETURN_REPORT_CATEGORY_REQUEST;
			}
						
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
								
			}else{//message send failed
				//TODO verify extream situation
			}
			
		
	}

	
}
	
	

}
